package tree_util;

import com.common.MoleculeTool;
import com.common.PrimTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.LogTool;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/9.
 */
public class Op_Prim extends ActionSupport {

    private int id;
    private int moleculeID;
    private String primType;
    private String postSeq;
    private String reverseSeq;
    private String flag;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getMoleculeID() {
        return moleculeID;
    }

    public void setMoleculeID(int moleculeID) {
        this.moleculeID = moleculeID;
    }

    public String getPrimType() {
        return primType;
    }

    public void setPrimType(String primType) {
        this.primType = primType;
    }

    public String getPostSeq() {
        return postSeq;
    }

    public void setPostSeq(String postSeq) {
        this.postSeq = postSeq;
    }

    public String getReverseSeq() {
        return reverseSeq;
    }

    public void setReverseSeq(String reverseSeq) {
        this.reverseSeq = reverseSeq;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        PrimTool tool = (PrimTool) context.getBean("primTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();

        if (flag.equals("insert")) {
            id = tool.insert(primType, postSeq, reverseSeq, moleculeID);
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Prim", 1);
            writer.write(String.valueOf(id));
            writer.close();
        } else if (flag.equals("update")) {
            int column = tool.update(primType, postSeq, reverseSeq, id);
            LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Prim", 1);
            if (column == 1){
                writer.write("true");
            }
            writer.close();
        }
        return null;
    }

}
